package org.odst.tag;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.dom.OdfNamespaceNames;
import org.odst.exception.ParsePropertyException;
import org.odst.transformation.ResultTransformation;
import org.odst.transformer.SheetTransformer;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;

/**
 * @author Leonid Vysochyn
 * @author Cyril PODER
 */
public class OutlineTag extends BaseTag{
    protected final Log log = LogFactory.getLog(getClass());

    public static final String TAG_NAME = "outline";

    public OutlineTag() {
        name = TAG_NAME;
    }

    private boolean detail;

    public boolean isDetail() {
        return detail;
    }

    public void setDetail(boolean detail) {
        this.detail = detail;
    }

    public ResultTransformation process(SheetTransformer sheetTransformer) {
        if( log.isDebugEnabled() ){
            log.info("jx:outline tag processing..");
        }

        Block body = tagContext.getTagBody();
        if( body.getNumberOfRows()==1 ){
            log.warn("jx:outline for columns is not supported. Ignoring.");
        }
        int shiftNumber = 0;

        ResultTransformation shift = new ResultTransformation(0);
        tagContext.getSheetTransformationController().removeBorders(body);
        shiftNumber += -2;
        try {
            ResultTransformation processResult = sheetTransformer.processRows(tagContext.getSheetTransformationController(), body.getStartRowNum(), body.getEndRowNum(), null );
            if( body.getStartRowNum() <= body.getEndRowNum() + processResult.getLastRowShift() ){
                groupRows( body.getStartRowNum(), body.getEndRowNum() + processResult.getLastRowShift() );
            }
            shift.add( processResult );
        } catch (ParsePropertyException e) {
            log.error("Can't parse property ", e);
        }

        return shift.add( new ResultTransformation(0, shiftNumber) );
    }

    private void groupRows(int startRowNum, int endRowNum) {
        OdfTable oooSheet = tagContext.getSheet().getOooSheet();
        OdfTableRow firstRow = oooSheet.getRowByIndex(startRowNum);
        Node tableRowGroup = oooSheet.getOdfElement().getOwnerDocument().createElementNS(OdfNamespaceNames.TABLE.getUri(), "table:table-row-group");
        oooSheet.getOdfElement().insertBefore(tableRowGroup, firstRow.getOdfElement());
        OdfTableRow row = firstRow;
        for (int i=startRowNum;i<=endRowNum;i++) {
        	OdfTableRow nextRow = row.getNextRow();
        	tableRowGroup.appendChild(oooSheet.getOdfElement().removeChild(row.getOdfElement()));
        	row = nextRow;
        }
        Attr display = oooSheet.getOdfElement().getOwnerDocument().createAttributeNS(OdfNamespaceNames.TABLE.getUri(), "table:display");
        display.setNodeValue(detail ? "true" : "false");
        tableRowGroup.getAttributes().setNamedItem(display);
    }
}
